import 'dart:math';

import 'package:cash_drawer/common/application.dart';
import 'package:cash_drawer/common/user.dart';
import 'package:cash_drawer/data/apis_service.dart';
import 'package:cash_drawer/data/model/base_model.dart';
import 'package:cash_drawer/event/login_event.dart';
import 'package:cash_drawer/ui/login_page.dart';
import 'package:cash_drawer/ui/main_page.dart';
import 'package:cash_drawer/utils/screen_adapter.dart';
import 'package:cash_drawer/utils/utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';


/// 启动页面
class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _SplashPageState();
  }
}

class _SplashPageState extends State<SplashPage>{
  String username;
  bool isLogin = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    var today = DateTime.now();
    print('当前时间是：$today');


    Future.delayed(Duration(seconds:2), () {
      apiService.getKey((BaseModel model){

        if (null != User.singleton.userName && User.singleton.userName.isNotEmpty) {
          isLogin = true;
          username = User.singleton.userName;
        }
        registerLoginEvent();
        Future.delayed(Duration(seconds: 2), () {
          Navigator.of(context).pushAndRemoveUntil(
              new MaterialPageRoute(builder: (context) => isLogin?MainPage():LoginPage()),
                  (route) => route == null);
        });
      },(DioError error){
        new AlertDialog(
          // title: new Text(''),
          content: new Text('确定删除吗？'),
          actions: <Widget>[
            new FlatButton(
              onPressed: () {
                Navigator.of(context).pop(false);
                pop();
              },
              child:
              new Text('确定', style: TextStyle(color: Colors.cyan)),
            ),
          ],
        );
      });

    });
  }

  static Future<void> pop() async {
    await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }

  void registerLoginEvent() {
    Application.eventBus.on<LoginEvent>().listen((event) {
      setState(() {
        isLogin = true;
        username = User.singleton.userName;
      });
    });
  }



  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return new Container(
        color: Theme.of(context).primaryColor,
        // ThemeUtils.dark ? Color(0xFF212A2F) : Colors.grey[200],
        alignment: Alignment.center,
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Card(
              elevation: 0,
              color: Colors.transparent,
              margin: EdgeInsets.all(2.0),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(1.0))),
              child: new Image(image: new AssetImage(Utils.getImgPath("ic_launcher_web"))),
//              CircleAvatar(
//                backgroundColor: Colors.transparent,
//                backgroundImage: AssetImage(Utils.getImgPath('ic_launcher_web')),
//                radius: 100.0,
//              )
            ),
          ],
        ),
    );
  }


}